import { request } from '@/utils/request'
import { findByStorageKey } from '@/utils/localStorageUtil'

//根据分类 id 获取属性以及属性值
export const getAttrListByCategoryId = (categoryId) => {
  return request.get(`/attr/${categoryId}`, {
    headers: {
      accessToken: findByStorageKey('accessToken')
    }
})
}

// 获取属性列表
export const getAttrList = (data) => {
  return request.get(`/attrValue/${data}`, {
    headers: {
      accessToken: findByStorageKey('accessToken')
    }
  })
}

//添加或更新属性
export const addOrDeleteAttr = (data: any, extend: any) => {
  if (data.id) {
    //如果mem有值，则将其插入到formData.value.attrValueList中
    if (extend) {
      data.attrValueList.push(...extend)
    }
    return request.put('/attr/update', data, {
      headers: {
        accessToken: findByStorageKey('accessToken')
      }
    })
  } else {
    return request.post('/attr/save', data, {
      headers: {
        accessToken: findByStorageKey('accessToken')
      }
    })
  }
}

//删除属性
export const deleteAttrByAttrId = (data) => {
  return request.delete(`/attr/delete/${data}`, {
    headers: {
      accessToken: findByStorageKey('accessToken')
    }
  })
}
